﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class ChatSessionsDao
    {
        public bool Delete(SqlConnection conn, int sessionId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("DELETE FROM [dbo].[Chat_Messages] ");
            builder.Append("WHERE ");
            builder.Append("\t[SessionId] = @SessionId ");
            builder.Append("DELETE FROM [dbo].[Chat_User] ");
            builder.Append("WHERE ");
            builder.Append("\t[SessionId] = @SessionId ");
            builder.Append("DELETE FROM [dbo].[Chat_Sessions] ");
            builder.Append("WHERE ");
            builder.Append("\t[SessionId] = @SessionId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@SessionId", SqlDbType.Int) };
            commandParameters[0].Value = sessionId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public bool Insert(SqlConnection conn, ChatSessionsData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("INSERT [dbo].[Chat_Sessions] ");
            builder.Append("( ");
            builder.Append("    [SessionName], ");
            builder.Append("    [State] ");
            builder.Append(") ");
            builder.Append("VALUES ");
            builder.Append("( ");
            builder.Append("    @SessionName, ");
            builder.Append("    @State ");
            builder.Append(") ");
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@SessionName", SqlDbType.NVarChar, 100);
            commandParameters[0].Value = data.SessionName;
            commandParameters[1] = new SqlParameter("@State", SqlDbType.Bit);
            commandParameters[1].Value = data.State;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public DataTable SelectAll(SqlConnection conn)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("UPDATE [dbo].[Chat_User] ");
            builder.Append("SET ");
            builder.Append("\t[State]=0 ");
            builder.Append("WHERE ");
            builder.Append("\tDATEDIFF(s,[LastRefreshTime],getdate())>120  ");
            builder.Append("DECLARE @SessionId int ");
            builder.Append("DECLARE Online_Cursor CURSOR FOR  ");
            builder.Append("\t\tSELECT [SessionId] FROM [dbo].[Chat_Sessions] ");
            builder.Append("OPEN Online_Cursor ");
            builder.Append("FETCH NEXT FROM Online_Cursor INTO @SessionId ");
            builder.Append("WHILE @@FETCH_STATUS = 0 ");
            builder.Append("BEGIN ");
            builder.Append("\tUPDATE [dbo].[Chat_Sessions] ");
            builder.Append("\tSET ");
            builder.Append("\t\t[OnlineNums] = (SELECT COUNT(*) FROM [dbo].[Chat_User] WHERE [SessionId] = @SessionId AND [State]=1)  ");
            builder.Append("\tWHERE ");
            builder.Append("\t\t[SessionId] = @SessionId ");
            builder.Append("\tFETCH NEXT FROM Online_Cursor INTO @SessionId ");
            builder.Append("END ");
            builder.Append("CLOSE Online_Cursor ");
            builder.Append("DEALLOCATE Online_Cursor ");
            builder.Append("SELECT ");
            builder.Append("\t[SessionId], ");
            builder.Append("\t[SessionName], ");
            builder.Append("\t[State], ");
            builder.Append("\t[OnlineNums] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Chat_Sessions] ");
            builder.Append("ORDER BY ");
            builder.Append("\t[SessionId] ASC ");
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString());
            set.Tables[0].TableName = "Chat_Sessions";
            return set.Tables[0];
        }

        public DataTable SelectBySessionId(SqlConnection conn, int sessionId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append("\t[SessionId], ");
            builder.Append("\t[SessionName], ");
            builder.Append("\t[State] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Chat_Sessions] ");
            builder.Append("WHERE ");
            builder.Append("\t[SessionId] = @SessionId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@SessionId", SqlDbType.Int) };
            commandParameters[0].Value = sessionId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Chat_Sessions";
            return set.Tables[0];
        }

        public bool Switch(SqlConnection conn, int sessionId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("DECLARE @State int ");
            builder.Append("SELECT ");
            builder.Append("\t@State =[State] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Chat_Sessions] ");
            builder.Append("WHERE ");
            builder.Append("\t[SessionId] = @SessionId ");
            builder.Append("IF @State = 1 ");
            builder.Append("\tBEGIN ");
            builder.Append("\t\tDELETE FROM [dbo].[Chat_Messages] WHERE [SessionId] = @SessionId ");
            builder.Append("\t\tINSERT [dbo].[Chat_Messages](SessionId, Color, Content, SendTime) VALUES (@SessionId,'black','聊天室已经关闭',GETDATE()) ");
            builder.Append("\tEND ");
            builder.Append("ELSE ");
            builder.Append("\tDELETE FROM [dbo].[Chat_Messages] WHERE [SessionId] = @SessionId ");
            builder.Append("\t ");
            builder.Append("UPDATE [dbo].[Chat_Sessions] ");
            builder.Append("SET ");
            builder.Append("\t[State] = ABS([State]-1), ");
            builder.Append("\t[OnlineNums] = 0 ");
            builder.Append("WHERE ");
            builder.Append("\t[SessionId] = @SessionId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@SessionId", SqlDbType.Int) };
            commandParameters[0].Value = sessionId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }
    }
}

